package com.example.teatea.controller;

import com.example.teatea.dao.DoFindStaff;
import com.example.teatea.dao.FindAll;
import com.example.teatea.project.Staff;
import com.example.teatea.tools.ListTools;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import javafx.util.Callback;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

public class TextController implements Initializable {
    public TableColumn name;
    public TableColumn sex;
    public TableColumn phone;
    public TableColumn time;
    public TableView view;
    public Pagination page;
    public TextField phoneFind;
    public TextField nameFind;
    public List<Staff> stuInfos;
    public int total;//总记录数
    public int pagesize = 15;//每页显示的条数
    public int pagecount;//总页数

    public void toFInd(ActionEvent actionEvent) throws IOException {
        //根据搜索文本框的值获取数据库的数据
        stuInfos = new DoFindStaff().findStaffByPhone(phoneFind.getText());
        total=stuInfos.size();//获取总记录条数
        pagecount=(total+(pagesize-1))/pagesize;//得到总页数
        page.setPageCount(pagecount);//设置分页组件的总页数
        //绑定分页组件中分页按钮的点击
        page.setPageFactory(new Callback<Integer, Node>() {
            @Override
            public Node call(Integer pagenum) {//点击分页按钮的回调函数
                loadCurPageData(pagenum);
                return view;//返回装载当前页数据的tableview
            }
        });
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
       initTableView();
    }

    public void initTableView(){
        stuInfos = new DoFindStaff().findStaffByName(nameFind.getText());
        total = stuInfos.size();
        pagecount = (total + (pagesize - 1)) / pagesize;
        page.setPageCount(pagecount);
        page.setPageFactory(new Callback<Integer, Node>() {
            @Override
            public Node call(Integer integer) {
                loadCurPageData(integer);
                return view;
            }
        });
    }

    /**
     * 页面切换调用该方法
     *
     * @param pageIndex 当前页码
     * @return
     */
    public void loadCurPageData(int pageIndex) {
        List<Staff> subList = (List<Staff>) ListTools.pagingList(stuInfos,15,pageIndex);
        ObservableList<Staff> data = FXCollections.observableArrayList(subList);
        name.setCellValueFactory(new PropertyValueFactory<>("sName"));
        sex.setCellValueFactory(new PropertyValueFactory<>("sSex"));
        phone.setCellValueFactory(new PropertyValueFactory<>("sPhone"));
        time.setCellValueFactory(new PropertyValueFactory<>("sTime"));
        view.setItems(data);
    }

    public void toFInd1(ActionEvent actionEvent) {
        initTableView();
    }
}